package com.ybg.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

public class DateTimeUtil {
	public static final String YYYY_MM = "yyyy-MM";
	public static final String YYYY_MM_DD = "yyyy-MM-dd";
	public static final String HH_MM_SS = "HH:mm:ss";
	public static final String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
	public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
	public static final long MILLIS_IN_A_DAY = 24 * 60 * 60 * 1000;
	public static final SimpleDateFormat SHORT_TIME=new SimpleDateFormat("HH:mm");
	public static final SimpleDateFormat FULL_TIME=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public static final SimpleDateFormat HH_MM_SS2=new SimpleDateFormat("HH:mm:ss");

	public static String getYestoday(String pattern) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DAY_OF_YEAR, -1);
		String dateTime = sdf.format(cal.getTime());
		return dateTime;
	}

	/**
	 * wgl
	 * 
	 * @param pattern
	 * @return
	 */
	public static String getToday(String pattern) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		Calendar cal = Calendar.getInstance();
		String dateTime = sdf.format(cal.getTime());
		return dateTime;
	}

	/**
	 * wgl
	 * 
	 * @param pattern
	 * @return
	 */
	public static String getTomorrow(String pattern) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DAY_OF_YEAR, +1);
		String dateTime = sdf.format(cal.getTime());
		return dateTime;
	}

	public static String getLastSaturday(String pattern){
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DAY_OF_YEAR, -7);
		cal.set(Calendar.DAY_OF_WEEK,Calendar.SATURDAY);
		String dateTime = sdf.format(cal.getTime());
		return dateTime;
	}

	/**
	 * wgl 得到距离当前num天的日期
	 * 
	 * @param pattern
	 * @return
	 */
	public static String getDateStrDiffToday(String pattern, int num) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DAY_OF_YEAR, num);
		String dateTime = sdf.format(cal.getTime());
		return dateTime;
	}

	/**
	 * wgl 获得当前月--开始日期
	 * 
	 * @param date
	 * @return
	 */
	public static String getMinMonthDate(String date) {
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		try {
			calendar.setTime(format.parse(date));
			calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
			return format.format(calendar.getTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		System.out.println("DateTimeUtil时间转换出现问题--------获得当前月--开始日期");
		return null;
	}

	/**
	 * wgl 获得当前月--结束日期
	 *
	 * @param date
	 * @return
	 */
	public static String getMaxMonthDate(String date) {
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		try {
			calendar.setTime(format.parse(date));
			calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
			return format.format(calendar.getTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		System.out.println("DateTimeUtil时间转换出现问题--------获得当前月--结束日期");
		return null;
	}

	public static List<String> getDateRangeFormate(long begin, long end, long scale, String pattern) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(begin);
		List<String> dateList = new LinkedList<String>();
		for (long l = begin; l < end; l += scale) {
			dateList.add(sdf.format(new Date(l)));
		}
		return dateList;
	}

	/**
	 * firstTimeBeforeSecond:(比较第一个时间是否在第二个时间之前). <br/>
	 *
	 * @date 2015年11月24日 下午6:01:04
	 * @author ChenShikang
	 * @version 1.0
	 * @since JDK 1.6
	 * @param firstTime
	 * @param secondTime
	 * @param pattern
	 * @return
	 * @throws ParseException
	 */
	public static boolean firstTimeBeforeSecond(String firstTime, String secondTime, String pattern)
			throws ParseException {
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		Date firstDate = null;
		Date secondDate = null;
		firstDate = format.parse(firstTime);
		secondDate = format.parse(secondTime);
		return firstDate.before(secondDate);
	}

	/**
	 * firstTimeAfterSecond:(比较第一个时间是否在第二个时间之后). <br/>
	 *
	 * @date 2015年11月24日 下午6:01:45
	 * @author ChenShikang
	 * @version 1.0
	 * @since JDK 1.6
	 * @param firstTime
	 * @param secondTime
	 * @param pattern
	 * @return
	 * @throws ParseException
	 */
	public static boolean firstTimeAfterSecond(String firstTime, String secondTime, String pattern)
			throws ParseException {
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		Date firstDate = null;
		Date secondDate = null;
		firstDate = format.parse(firstTime);
		secondDate = format.parse(secondTime);
		return firstDate.after(secondDate);
	}

	/**
	 * getDayByDistance:(获取某天之前或者之后的n天). <br/>
	 *
	 * @date 2015年11月24日 下午6:03:48
	 * @author ChenShikang
	 * @version 1.0
	 * @since JDK 1.6
	 * @param oneday
	 * @param pattern
	 * @return
	 * @throws ParseException
	 */
	public static String getDayByDistance(String oneday, int n, String pattern) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		Calendar cal = Calendar.getInstance();
		cal.setTime(sdf.parse(oneday));
		cal.add(Calendar.DAY_OF_YEAR, n);
		String dateTime = sdf.format(cal.getTime());
		return dateTime;
	}

	public static String formatDateTime(String dateTime, String pattern) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		Calendar cal = Calendar.getInstance();
		cal.setTime(sdf.parse(dateTime));
		return sdf.format(cal.getTime());
	}

	/**
	 * getTimeByPattern:(获取指定格式的时间). <br/>
	 *
	 * @date 2015年11月25日 下午2:04:53
	 * @author ChenShikang
	 * @version 1.0
	 * @since JDK 1.6
	 * @param time
	 * @param timePattern
	 * @param patternToget
	 * @return
	 * @throws ParseException
	 */
	public static String getTimeByPattern(String time, String timePattern, String patternToget) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat(timePattern);
		SimpleDateFormat sdf2 = new SimpleDateFormat(patternToget);
		Calendar cal = Calendar.getInstance();
		cal.setTime(sdf.parse(time));
		return sdf2.format(cal.getTime());
	}

	/**
	 * daysBetween:(日期天数之差). <br/>
	 *
	 * @date 2015年12月23日 上午10:09:22
	 * @author ChenShikang
	 * @version 1.0
	 * @since JDK 1.6
	 * @param smdate
	 * @param bdate
	 * @return
	 * @throws ParseException @throws
	 */
	public static int daysBetween(String smdate, String bdate, String pattern) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		Calendar cal = Calendar.getInstance();
		cal.setTime(sdf.parse(smdate));
		long time1 = cal.getTimeInMillis();
		cal.setTime(sdf.parse(bdate));
		long time2 = cal.getTimeInMillis();
		long between_days = (time2 - time1) / (1000 * 3600 * 24);
		return Integer.parseInt(String.valueOf(between_days));
	}

	/**
	 * @param consumeTime yyyy-MM-dd HH:mm:ss
	 * @return 时间格式统一为:yyyy-MM-dd
	 */
	public static String converConsumeTimeToDate(String consumeTime) {
		String blank = " ";
		String oldestDate = consumeTime.split(blank)[0];// 获得日期部分
		return oldestDate;
	}

	public static void main(String[] args) throws Exception {
		// System.out.println(DateTimeUtil.firstTimeBeforeSecond(null, null,
		// DateTimeUtil.HH_MM_SS ));
		// System.out.println(DateTimeUtil.firstTimeAfterSecond("7:8:59",
		// "7:10:59", DateTimeUtil.HH_MM_SS ));
		// System.out.println(DateTimeUtil.firstTimeBeforeSecond("7:8:59",
		// "7:8:54", DateTimeUtil.HH_MM_SS ));
		// System.out.println(DateTimeUtil.getYestoday(DateTimeUtil.YYYY_MM_DD));
		// System.err.println(DateTimeUtil.getDayByDistance(DateTimeUtil.getToday(DateTimeUtil.YYYY_MM_DD),
		// -2, DateTimeUtil.YYYY_MM_DD));
		// System.out.println(DateTimeUtil.getDayByDistance("2015-11-24", -3,
		// DateTimeUtil.YYYY_MM_DD));
		// System.out.println(DateTimeUtil.getTimeByPattern("2012-11-11 8:8:8",
		// DateTimeUtil.YYYY_MM_DD_HH_MM_SS, DateTimeUtil.HH_MM_SS));
		// System.out.println(daysBetween("2015-11-22","2015-11-23",YYYY_MM_DD));
		// System.out.println(daysBetween("2015-11-27","2015-11-23",YYYY_MM_DD));
		System.err.println(getToday(YYYY_MM_DD_HH_MM_SS));
	}
}